package com.atguigu.lease.web.app.interceptors;

import com.atguigu.lease.common.jwt.JwtUtil;
import com.atguigu.lease.common.result.ResultCodeEnum;
import com.atguigu.lease.common.springmvc.LeaseException;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.util.ObjectUtils;
import org.springframework.web.servlet.HandlerInterceptor;

/**
 * projectName: com.atguigu.lease.web.app.interceptors
 *
 * @author: 吴振鹏
 * description:
 * time: 2025/7/14 11:33
 */

public class LoginInterceptor implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        // 获取请求头中的access_token
        String accessToken = request.getHeader("access_token");
        // 判断access_token是否为空
        if(ObjectUtils.isEmpty(accessToken)){
            throw new LeaseException(ResultCodeEnum.APP_LOGIN_AUTH);
        }
        // 解析access_token,检查是否有效过期
        JwtUtil.parseToken(accessToken);
        return true;
    }
}
